import * as realm_property from "../system/realm_property";
import * as derived_tools from "../system/derived_tools";
import * as math_tools from "../system/math_tools";
export function Pouring(object) {
    try {
        const getPermutation = object.permutation;
        const getCacheValue = getPermutation.getState('STATE:magma');
        const getStoneValue = getPermutation.getState('STATE:material');
        const getProtoArray = [];
        const portPositive_X = object.offset({ x: 1, y: -1, z: 0 });
        const portNegative_X = object.offset({ x: -1, y: -1, z: 0 });
        const portNegative_Y = object.offset(math_tools.Vector.CONSTANT_DOWN);
        const portPositive_Z = object.offset({ x: 0, y: -1, z: 1 });
        const portNegative_Z = object.offset({ x: 0, y: -1, z: -1 });
        if (portPositive_X)
            getProtoArray.push(portPositive_X);
        if (portNegative_X)
            getProtoArray.push(portNegative_X);
        if (portPositive_Z)
            getProtoArray.push(portPositive_Z);
        if (portNegative_Z)
            getProtoArray.push(portNegative_Z);
        const getBlockArray = getProtoArray.filter(info => info.hasTag('data_tag:obsidian_smelting.storage_tank'));
        let lavaValue = 0;
        getBlockArray.forEach(info => {
            const getInfoStates = info.permutation;
            const getInfoValue = getInfoStates.getState('STATE:magma');
            if (getInfoValue > 13)
                return;
            info.setPermutation(getInfoStates.withState('STATE:magma', getInfoValue + 2));
            lavaValue += 2;
        });
        object.setPermutation(getPermutation.withState('STATE:magma', getCacheValue - lavaValue));
        const container = portNegative_Y?.getComponent('minecraft:inventory')?.container;
        const consume = 8 - getStoneValue;
        if (!container)
            return;
        for (let index = 0; index < container.size; index++) {
            const item = container.getItem(index);
            if (!item || item.amount < consume || item.typeId != 'block_unit:compressed_stone')
                continue;
            if (item.amount > consume) {
                item.amount -= consume;
                container.setItem(index, item);
            }
            else
                container.setItem(index);
            object.setPermutation(object.permutation.withState('STATE:material', 8));
            break;
        }
    }
    catch (error) {
        derived_tools.ErrorMessage('<§l§b 曜石熔炼 §r>§4 发生错误§r', object, { text: error.message });
    }
    ;
}
;
export function Attrition(object) {
    if (!realm_property.ExpendEnergy(object, -45))
        return;
    const getPermutation = object.permutation;
    object.setPermutation(getPermutation.withState('STATE:stage', 1));
}
